﻿// -----------------------------------------------------------------------
// <copyright file="Recipe.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using WSU.PMP.BE.DATA.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using WSU.PMP.BE.DATA.Abstract;

namespace WSU.PMP.BE.DATA.Concrete
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Recipe : IRecipe
    {
        public EfRecipe getRecipeById(string recipeId)
        {
            using (var db = new PmpEntities())
            {
                long key = long.Parse(recipeId);

                EfRecipe recipe = (from i in db.EfRecipes
                               where i.RecipeID == key
                               select i).FirstOrDefault();

                return recipe;
            }
        }

        public EfRecipe getRecipeByName(string name)
        {
            using (var db = new PmpEntities())
            {
                EfRecipe recipe = (from i in db.EfRecipes
                                   where i.Name == name
                                   select i).FirstOrDefault();

                return recipe;
            }
        }

        public List<EfRecipe> getRecipesByName(string name)
        {
            using (var db = new PmpEntities())
            {
                IQueryable<EfRecipe> recipes = from i in db.EfRecipes
                                         where i.Name.Contains(name)
                                         select i;

                return recipes.ToList();
            }
        }

        public List<EfRecipe> getRecipesByDate(DateTime date)
        {
            using (var db = new PmpEntities())
            {
                IQueryable<EfRecipe> recipes = from i in db.EfRecipes
                                               where i.Date.ToShortDateString() == date.ToShortDateString()
                                               select i;

                return recipes.ToList();
            }
        }

        public List<EfRecipe> getRecipesByDateRange(DateTime start, DateTime end)
        {
            using (var db = new PmpEntities())
            {
                IQueryable<EfRecipe> recipes = from i in db.EfRecipes
                                               where i.Date >= start && i.Date < end
                                               select i;

                return recipes.ToList();
            }
        }

        public List<EfRecipe> getRecipesByItem(string barcode)
        {
            using (var db = new PmpEntities())
            {
                long key = long.Parse(barcode);

                IQueryable<EfRecipe> recipes = from r in db.EfRecipes
                                               join ri in db.EfRecipeItems on r.RecipeID equals ri.RecipeID
                                               join i in db.EfItems on ri.Item.Barcode equals i.Barcode
                                               where i.Barcode == key
                                               select r;

                return recipes.ToList();
            }
        }

        public List<EfRecipe> getRecipesByItemName(string name)
        {
            using (var db = new PmpEntities())
            {
                IQueryable<EfRecipe> recipes = from r in db.EfRecipes
                                               join ri in db.EfRecipeItems on r.RecipeID equals ri.RecipeID
                                               join i in db.EfItems on ri.Item.Barcode equals i.Barcode
                                               where i.Name.Contains(name)
                                               select r;

                return recipes.ToList();
            }
        }

        public void addRecipe(EfRecipe recipe)
        {
            using (var db = new PmpEntities())
            {
                db.EfRecipes.AddObject(recipe);
                db.SaveChanges();
            }
        }

        public void editRecipe(EfRecipe recipe)
        {
            using (var db = new PmpEntities())
            {
                db.EfRecipes.Attach(db.EfRecipes.Single(i => i.RecipeID == recipe.RecipeID));
                db.EfRecipes.ApplyCurrentValues(recipe);
                db.SaveChanges();
            }
        }

        public void deleteRecipe(EfRecipe recipe)
        {
            using (var db = new PmpEntities())
            {
                db.EfRecipes.Attach(recipe);
                db.EfRecipes.DeleteObject(recipe);
                db.SaveChanges();
            }
        }
    }
}
